package com.cntaiping.workutil.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

public class FileUtils {
    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);


    public static String getFileContent(String filePath) {
//        System.out.println("final filePath="+filePath);
        StringBuffer buffer = new StringBuffer();
        String s1 = "";
        try (
                BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))) {
            String s = null;
            while ((s = bf.readLine()) != null) {
                buffer.append(s.trim());
            }
            s1 = buffer.toString();
        } catch (Exception e) {
            logger.error("读取文件" + filePath + "失败:", e);
        }
        return s1;
    }

    public static void setFileContent(String filePath,String content) {
//        System.out.println("final filePath="+filePath);

        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) {
            bw.write(content);
            bw.flush();

        } catch (Exception e) {
            logger.error("写入文件" + filePath + "失败:", e);
        }
    }

    public List<String> readFile(String filePath) throws Exception {
        ArrayList<String> list = new ArrayList<>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(filePath);
            isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
            br = new BufferedReader(isr);
            String line = "";
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            logger.error("reader txt file error=", e);
        } finally {
            br.close();
            isr.close();
            fis.close();
        }
        return list;
    }

    public static String getTransFile(String jsonFilePath) {
        jsonFilePath = jsonFilePath.replaceAll("\\\\", Matcher.quoteReplacement(File.separator));
        jsonFilePath = jsonFilePath.replaceAll("/", Matcher.quoteReplacement(File.separator));
        return jsonFilePath;
    }

    public static <T> T loadYaml(String path, Class<T> clazz) {
        InputStream in = null;
        try {
            Yaml yaml = new Yaml();
            // yaml文件路径
            in = new FileInputStream(path);
            return yaml.loadAs(in, clazz);

        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        }
    }

    public static String changePath(String suffix) {
        if (!org.apache.commons.lang3.StringUtils.isBlank(suffix)) {
            if (!suffix.startsWith(".")) {
                if(!suffix.startsWith(File.separator)){
                    suffix = "./" + suffix;
                }else {
                    suffix = "." + suffix;
                }

            }
            suffix = FileUtils.getTransFile(suffix);
        }
        return suffix;
    }
}
